
local RUtil = require 'packages/xiaobai/utility/_base'

local U = require "packages/utility/utility"

--- 重新隐匿
---@param player ServerPlayer @ 目标角色
RUtil.EnterHidden = function (player)
  local room = player.room
  room:sendLog({
    type = "#rfenghouEnterHidden",
    from = player.id,
  })
  local skills = "hidden_skill&"
  room:setPlayerMark(player, "__hidden_general", player.general)
  for _, s in ipairs(Fk.generals[player.general]:getSkillNameList(true)) do
    if player:hasSkill(s, true) then
      skills = skills.."|-"..s
    end
  end
  if player.deputyGeneral ~= "" then
    room:setPlayerMark(player, "__hidden_deputy", player.deputyGeneral)
    for _, s in ipairs(Fk.generals[player.deputyGeneral]:getSkillNameList(true)) do
      if player:hasSkill(s, true) then
        skills = skills.."|-"..s
      end
    end
  end
  player.general = "hiddenone"
  player.gender = General.Male
  room:broadcastProperty(player, "gender")
  if player.deputyGeneral ~= "" then
    player.deputyGeneral = ""
  end
  player.kingdom = "jin"
  room:setPlayerMark(player, "__hidden_record",
  {
    maxHp = player.maxHp,
    hp = player.hp,
  })
  player.maxHp = 1
  player.hp = 1
  for _, property in ipairs({"general", "deputyGeneral", "kingdom", "maxHp", "hp"}) do
    room:broadcastProperty(player, property)
  end
  room:handleAddLoseSkills(player, skills, nil, false, true)
end
Fk:loadTranslationTable{
  ["#rfenghouEnterHidden"] = "%from 进入隐匿状态",
}






--印影
RUtil.getShade = function (room, n)
  local ids = {}
  for _, id in ipairs(room.void) do
    if n <= 0 then break end
    if Fk:getCardById(id).name == "rfenghou__shade" then
      room:setCardMark(Fk:getCardById(id), MarkEnum.DestructIntoDiscard, 1)
      table.insert(ids, id)
      n = n - 1
    end
  end
  while n > 0 do
    local card = room:printCard("rfenghou__shade", Card.Spade, 1)
    room:setCardMark(card, MarkEnum.DestructIntoDiscard, 1)
    table.insert(ids, card.id)
    n = n - 1
  end
  return ids
end

--先把半成品蓄谋放在这里，祭祀智慧不想加jsrg……
---@param player ServerPlayer @ 操作蓄谋的玩家
---@param card integer | Card  @ 用来蓄谋的牌
---@param skill_name? string @ 技能名
---@param proposer? integer @ 移动操作者的id
---@return nil
RUtil.premeditate = function(player, card, skill_name, proposer)
  skill_name = skill_name or ""
  proposer = proposer or player.id
  local room = player.room
  if type(card) == "table" then
    assert(not card:isVirtual() or #card.subcards == 1)
    card = card:getEffectiveId()
  end
  local xumou = Fk:cloneCard("rfenghou__premeditate")
  xumou:addSubcard(card)
  room:sendLog{
    type = "#premeditate",
    from = proposer,
    to = {player.id},
  }
  player:addVirtualEquip(xumou)
  room:moveCardTo(xumou, Player.Judge, player, fk.ReasonJustMove, skill_name, nil, false, proposer, "", {proposer, player.id})
end



Fk:loadTranslationTable{
  ["#premeditate"] = "%from 对 %to 进行了蓄谋",
  ["#premeditate-use"] = "你可以使用此蓄谋牌%arg，或点“取消”将所有蓄谋牌置入弃牌堆",
}

--- 更改转换技标记，适用于3项以上的diy转换技
---@param player ServerPlayer @ 目标角色
---@param total integer @ 转换技总项数
RUtil.RfenghouSwitch = function (player, total)
  local room = player.room
  for i = 1, 999, 1 do
    if player:getMark("@!rfenghou"..i) > 0 then
      room:setPlayerMark(player, "@!rfenghou"..i, 0)
      if i < total then
        room:setPlayerMark(player, "@!rfenghou"..(i + 1), 1)
      else
        room:setPlayerMark(player, "@!rfenghou1", 1)
      end
      return
    end
  end
end
return RUtil
